package post;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author 王宁 创建日期 Jul 30, 2008 模拟POST提交方式
 */
public class PostEmulator {
    private static final Log log = LogFactory.getLog(PostEmulator.class);
    private URL url;

    public URLConnection conn;

    /**
     * 设置提交地址
     * 
     * @param urlAddr:例如http://www.aaa.com/abc.jsp
     */
    public void setURL(String urlAddr) {

        try {
            url = new URL(urlAddr);
            conn = url.openConnection();

            conn.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            conn.setRequestProperty("Pragma:", "no-cache");
            conn.setRequestProperty("Cache-Control", "no-cache");

        } catch (MalformedURLException ex) {
            log.error(ex);
        } catch (IOException ex) {
            log.error(ex);
        }
    }

    /**
     * 设置提交参数
     * 
     * @param post:例如aa=123&bb=435
     */
    public void sendPost(String post) {
        conn.setDoInput(true);
        conn.setDoOutput(true);
        PrintWriter output = null;
        try {
            output = new PrintWriter(conn.getOutputStream());
            output.print(post);
        } catch (IOException e) {
            log.error(e);
        } finally {
            if (output != null)
                output.close();
        }

    }

    /**
     * 返回访问结果
     * 
     * @return html代码格式结果
     */
    public String getContent() {
        String line, result = "";
        try {
            conn.connect();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn
                    .getInputStream()));
            while ((line = in.readLine()) != null) {
                result = result + "\n" + line;
            }
            in.close();
        } catch (IOException ex) {
            log.error(ex);
        }
        return result;
    }

    /**
     * 根据文件内容，创建一个文件
     * 
     * @param filename:文件名
     * @param fileContent:文件文本内容
     */
    public void createFile(String filename, String fileContent) {
        FileOutputStream out = null;
        PrintStream p = null;

        try {
            out = new FileOutputStream(filename);
            p = new PrintStream(out);
            p.print(fileContent);
        } catch (Exception e) {
            log.error(e);
        } finally {
            if (p != null)
                p.close();
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }
        }

    }
    
    public static void autoCreate(List<String> urls){
        PostEmulator poster = new PostEmulator();
        for(String url : urls){
            poster.setURL(url);
            poster.sendPost("");
            String html = poster.getContent();

            
            poster.createFile("c:/index.html", html);
        }
    }

    public static void main(String[] args) {
        PostEmulator poster = new PostEmulator();
        poster.setURL("http://www.nc8888.com/index.jsp");
        poster.sendPost("");
        String html = poster.getContent();

        poster.createFile("c:/index.html", html);
    }
}
